/*头文件*/
//------------------------------------
#include "lottery.h"
//------------------------------------

/*使用文件，文件指针*/
//------------------------------------
extern char* user_info;
extern char* lottery_info;
extern char* lottery_man;
extern char* prize_pool;
extern FILE* f_prize_pool;
extern FILE* f_lottery_man;
//------------------------------------

/*全局变量声明*/
//------------------------------------
extern int QIHAO, perPrice_lottery, STATE,\
			NEXT_AWARD_POOL,GREAT_AWARD,\
			MIDDLE_AWARD,SMALL_AWARD,PRIZE_POOL;
//------------------------------------


/* 函数功能：界面
 * 形参：
 * 返回：
 */
void menu(void)
{
	/*系统清屏*/
	system("clear"); 
	/*欢迎界面*/
	printf("欢迎公证人登陆!\n");
	printf("-----------------------------------------\n");
	printf("1.开奖！\n"); 
	printf("2.(测使用)开奖！\n"); 
	printf("3.查看获奖者\n");
	printf("4.查看已开奖彩票信息\n");
	printf("5.查看未开奖彩票信息\n"); 
	printf("0.登出\n");
	printf("11.显示菜单\n");
	printf("99.退出系统\n");
	printf("-----------------------------------------\n");
	return;
}

void init(void)
{
	int ifempty=1; //查看如果文件没有内容
	f_lottery_man=fopen(lottery_man,"r");
	if(!f_lottery_man){perror("");exit(1);}
	if(fscanf(f_lottery_man,"%d %d %d",\
				&QIHAO,&perPrice_lottery,&STATE)>0)
		ifempty=0; 
	if(ifempty){QIHAO=0,perPrice_lottery=0,STATE=1;}
	fclose(f_lottery_man);
	
	f_prize_pool=fopen(prize_pool,"r");
	if(!f_prize_pool){perror("");exit(1);}
	fscanf(f_prize_pool,"%d",&PRIZE_POOL);
	fclose(f_prize_pool);
	
	GREAT_AWARD=PRIZE_POOL*0.5;
	MIDDLE_AWARD=PRIZE_POOL*0.2;
	SMALL_AWARD=PRIZE_POOL*0.1;
}

/* 函数功能：开奖——宣布开奖彩票号码，金额分配
 * 形参：彩票信息，获奖者链表数组
 * 返回：
 */
void issue(xLottery* plots,winner_list* winner_list_arr[],char* sissue, int manual_mode)
{
	/*载入彩票管理信息*/
	int ifempty=1; //查看如果文件没有内容
	f_lottery_man=fopen(lottery_man,"r");
	if(!f_lottery_man){perror("");exit(1);}
	
	if(fscanf(f_lottery_man,"%d %d %d",\
				&QIHAO,&perPrice_lottery,&STATE)>0)
		ifempty=0; 
	if(ifempty){QIHAO=0,perPrice_lottery=0,STATE=1;}
	fclose(f_lottery_man);
	
	/*若开奖状态为1,则不能开奖*/
	if(STATE){printf("已开奖！未发布新期号！\n");return;}
	
	/*若开奖状态为0,则开奖，修改开奖状态为1*/
	f_lottery_man=fopen(lottery_man,"r+");
	if(!f_lottery_man){perror("");exit(1);}
	fseek(f_lottery_man,-2,SEEK_END);
	fprintf(f_lottery_man,"%d",1);
	fclose(f_lottery_man);
	
	/*随机生成中奖号码*/
	if(!manual_mode){
	for(int i=0;i<LOTTERY_SIZE;++i)
	sissue[i]='0'+rand()%10;}

	/*手动生成中奖号码，测试用*/
	if(manual_mode){
	printf("请输入%d位中奖号码：",LOTTERY_SIZE);
	scanf("%s",sissue);}

printf("中奖号码为：%s！\n",sissue);

/*生成数组，保存各等奖获奖彩票在相应的链表*/
FILE* f_lot_info=fopen(lottery_info,"r");
if(!f_lot_info){perror("");exit(1);}
int i=LOTTERY_SIZE,end=LOTTERY_WIN_SIZE;
	
while(fscanf(f_lot_info,"%d %s %s %d %d %d %d",\
		&plots->id,plots->name,plots->lottery_num,\
		&plots->issue_version,&plots->issue_status,\
		&plots->prize_status,&plots->prize_gain)>0){
			//每一个彩票循环三次检查是否为某等奖彩票
			i=LOTTERY_SIZE,end=LOTTERY_WIN_SIZE;
			do{
				if( plots->issue_version==QIHAO \
				&& !strncmp(plots->lottery_num,sissue,i)){
				//printf("%s %s有%d个相同数字\n",plots->lottery_num,plots->name,i);
				list_insert(winner_list_arr[i-end],plots);
				break;
				}
			}while(--i>=end);
		}
fclose(f_lot_info);
}

/* 函数功能：分配金额
 * 形参：获奖者链表数组
 * 返回：
 */
void share(winner_list* winner_list_arr[],char* sissue)
{
	int i,sum=0; winner_list* cur_node;
	int award_price[LOTTERY_WIN_SIZE]={SMALL_AWARD,MIDDLE_AWARD,GREAT_AWARD};
	float award[LOTTERY_WIN_SIZE]={0};
	int count_winner[LOTTERY_WIN_SIZE]={0};
	
	i=LOTTERY_WIN_SIZE;
	while(i--){
		cur_node=winner_list_arr[i];
		//计算每个奖项有多少个获奖者
		while((cur_node=cur_node->next)) ++count_winner[i]; 
		//记录每个奖项的分配金额(按人数分)
		if(count_winner[i]) award[i] = award_price[i] / count_winner[i]; 
	}
	i=LOTTERY_WIN_SIZE;
	while(i--) sum+=award[i]*count_winner[i];
	NEXT_AWARD_POOL=PRIZE_POOL-sum;
	//修改奖池金额
	f_prize_pool=fopen(prize_pool,"w");
	if(!f_prize_pool){perror("");exit(1);}
	fprintf(f_prize_pool,"%d",NEXT_AWARD_POOL);
	fclose(f_prize_pool);
	//更新用户和彩票信息
	update_user_info(winner_list_arr,award);
	update_lottery_info(winner_list_arr,award);
}


/* 函数功能：修改用户信息和余额
 * 形参：获奖者链表数组,
 * 返回：
 */
void update_user_info(winner_list* winner_list_arr[],float award[])
{
	winner_list* cur_node; User users; 
	
	FILE* f_user=fopen(user_info,"r");
	if(!f_user){perror("");exit(1);}
	FILE* f_temp=tmpfile();
	if(!f_temp){perror("");exit(1);}
	//将原文件信息保存到临时文件进行处理
	while(fscanf(f_user,"%s %s %d",users.name,\
				users.pwd,&users.balance)>0)               //文件遍历
		fwrite(&users,sizeof(User),1,f_temp);
		
	int i=LOTTERY_WIN_SIZE;
	while(i--){ //链表数组遍历
		cur_node=winner_list_arr[i];
		while((cur_node=cur_node->next)){//链表遍历
			/*查找文件里获奖的那个人，将他金额增加*/
			rewind(f_temp);
		while(fread(&users,sizeof(User),1,f_temp))//二进制临时文件遍历
	{	
			if(!strcmp(users.name, cur_node->name))
		{ //将链表中姓名和文件中姓名比对，相等则增加相应金额
			users.balance += award[i];
			fseek(f_temp,-sizeof(User),SEEK_CUR); //文件指针回退一格
			fwrite(&users,sizeof(User),1,f_temp); //将修改的数据覆盖原来的数据
			break;
		}
	}}}
	
	fclose(f_user); rewind(f_temp);
	
	FILE* fp=fopen(user_info,"w");
	if(!fp){perror("");exit(1);}
	while(fread(&users,sizeof(User),1,f_temp)){
		//printf("%s %s %d\n",users.name,users.pwd,users.balance);
		fprintf(fp,"%s %s %d\n",users.name,users.pwd,users.balance);
	}
	fclose(fp);
}

/* 函数功能：修改彩票开奖信息，修改开奖状态，获得奖金，得到几等奖
 * 形参：获奖者链表数组,
 * 返回：
 */
void update_lottery_info(winner_list* winner_list_arr[],float award[])
{
	winner_list* cur_node; xLottery plots;
	
	FILE* f_lot_info=fopen(lottery_info,"r");
	if(!f_lot_info){perror("");exit(1);}
	FILE* lot_temp=tmpfile();
	if(!lot_temp){perror("");exit(1);}
	
	int i;
	//将原文件信息保存到临时文件进行处理
	while(fscanf(f_lot_info,"%d %s %s %d %d %d %d",\
		&plots.id,plots.name,plots.lottery_num,\
		&plots.issue_version,&plots.issue_status,\
		&plots.prize_status,&plots.prize_gain)>0)               //文件遍历
		{
		if(plots.issue_version==QIHAO)plots.issue_status=1;
		fwrite(&plots,sizeof(xLottery),1,lot_temp);
	}
		
	i=LOTTERY_WIN_SIZE;
	while(i--){ //链表数组遍历
		cur_node=winner_list_arr[i];
		while((cur_node=cur_node->next)){//链表遍历
			rewind(lot_temp);
		while(fread(&plots,sizeof(xLottery),1,lot_temp))//二进制临时文件遍历
	{	//查找id相同的彩票，修改彩票信息
			if( plots.id == cur_node->id )
		{ 
			plots.prize_status = i+1;
			plots.prize_gain =  award[i];
			fseek(lot_temp,-sizeof(xLottery),SEEK_CUR); //文件指针回退一格
			fwrite(&plots,sizeof(xLottery),1,lot_temp); //将修改的数据覆盖原来的数据
			break;
		}
	}}}
	
	fclose(f_lot_info); rewind(lot_temp);
	
	FILE* fp=fopen(lottery_info,"w");
	if(!fp){perror("");exit(1);}
	while(fread(&plots,sizeof(xLottery),1,lot_temp)){
		fprintf(fp,"%d %s %s %d %d %d %d\n",\
	plots.id,plots.name,plots.lottery_num,\
	plots.issue_version,plots.issue_status,\
	plots.prize_status,plots.prize_gain);
	}
	fclose(fp);
	
}


/* 函数功能：显示获奖者
 * 形参：从彩票信息文件读取获奖者信息
 * 返回：
 */
void show_winners(void)
{
	printf("-----------------------------------------\n");
	xLottery plots;
	FILE* f_lot_info=fopen(lottery_info,"r");
	if(!f_lot_info){perror("");exit(1);}

	int i=LOTTERY_WIN_SIZE;
	while(i--){
		printf("%d等奖获得者：\n",LOTTERY_WIN_SIZE-i);
		printf("ID\t彩票\t帐号\t获得奖金\n");
		rewind(f_lot_info);
	while(fscanf(f_lot_info,"%d %s %s %d %d %d %d",\
		&plots.id,plots.name,plots.lottery_num,\
		&plots.issue_version,&plots.issue_status,\
		&plots.prize_status,&plots.prize_gain)>0){
			
		if(plots.issue_version==QIHAO&&\
			plots.issue_status==1&&plots.prize_status==i+1)
			printf("%d\t%s\t%s\t%d\n",plots.id, plots.lottery_num,plots.name,plots.prize_gain);
		}
	}
	fclose(f_lot_info);
	printf("-----------------------------------------\n");
}


/* 函数功能：显示已或未开奖彩票信息
 * 形参：从彩票信息文件读取信息
 * 返回：
 */
void show_issued(int IFissued)
{
	printf("-----------------------------------------\n");
	xLottery plots;
	FILE* f_lot_info=fopen(lottery_info,"r");
	if(!f_lot_info){perror("");exit(1);}


		printf("彩票\t帐号\t期号\t开奖状态\t得奖状态\t获得奖金\n");
	while(fscanf(f_lot_info,"%d %s %s %d %d %d %d",\
		&plots.id,plots.name,plots.lottery_num,\
		&plots.issue_version,&plots.issue_status,\
		&plots.prize_status,&plots.prize_gain)>0){
			
		if(IFissued && plots.issue_status)
			printf("%s\t%s\t%d\t%d\t\t%d\t\t%d\n",\
		plots.lottery_num, plots.name,\
		plots.issue_version, plots.issue_status,\
		plots.prize_status, plots.prize_gain);
		
		if((!IFissued) && (!plots.issue_status))
			printf("%s\t%s\t%d\t%d\t\t%d\t\t%d\n",\
		plots.lottery_num, plots.name,\
		plots.issue_version, plots.issue_status,\
		plots.prize_status, plots.prize_gain);
	}
	fclose(f_lot_info);
	printf("-----------------------------------------\n");
}



/* 函数功能：链表插入获奖者
 * 形参：链表头，用户名
 * 返回：
 */
void list_insert(winner_list* head_node, xLottery* plots)
{
	winner_list* new_node=(winner_list*)calloc(1,sizeof(winner_list));
	strcpy(new_node->name,plots->name);
	strcpy(new_node->lottery_num,plots->lottery_num);
	new_node->id=plots->id;
	new_node->next = head_node->next;
	head_node->next = new_node;
}

/* 函数功能：查找获奖者
 * 形参：链表，用户名
 * 返回：找到返回1,未找到返回0
 */
int list_find(winner_list* cur_node, char name[USER_SIZE])
{
	while((cur_node=cur_node->next))
	{
		if(!strcmp(cur_node->name,name))return 1;
	}
	return 0;
}

